/*   1:    */ package org.intellij.lang.regexp.surroundWith;
/*   2:    */ 
/*   3:    */

import com.intellij.lang.ASTNode;
import com.intellij.lang.surroundWith.Surrounder;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.intellij.lang.regexp.RegExpFileType;
import org.intellij.lang.regexp.psi.RegExpAtom;
import org.intellij.lang.regexp.psi.RegExpPattern;
import org.intellij.lang.regexp.psi.impl.RegExpElementImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */ 
/*  22:    */ class GroupSurrounder
/*  23:    */   implements Surrounder
/*  24:    */ {
/*  25:    */   private final String a;
/*  26:    */   private final String b;
/*  27:    */   
/*  28:    */   public GroupSurrounder(String paramString1, String paramString2)
/*  29:    */   {
/*  30: 43 */     this.a = paramString1;
/*  31: 44 */     this.b = paramString2;
/*  32:    */   }
/*  33:    */   
/*  34:    */   public String getTemplateDescription()
/*  35:    */   {
/*  36: 48 */     return this.a;
/*  37:    */   }
/*  38:    */   
/*  39:    */   public boolean isApplicable(@NotNull PsiElement[] paramArrayOfPsiElement)
/*  40:    */   {
/*  41: 52 */     if (paramArrayOfPsiElement == null) {
/*  42: 52 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/lang/regexp/surroundWith/GroupSurrounder", "isApplicable" }));
/*  43:    */     }
/*  44: 52 */     return (paramArrayOfPsiElement.length == 1) || (PsiTreeUtil.findCommonParent(paramArrayOfPsiElement) == paramArrayOfPsiElement[0].getParent());
/*  45:    */   }
/*  46:    */   
/*  47:    */   @Nullable
/*  48:    */   public TextRange surroundElements(@NotNull Project paramProject, @NotNull Editor paramEditor, @NotNull PsiElement[] paramArrayOfPsiElement)
/*  49:    */     throws IncorrectOperationException
/*  50:    */   {
/*  51: 57 */     if (paramProject == null) {
/*  52: 57 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/lang/regexp/surroundWith/GroupSurrounder", "surroundElements" }));
/*  53:    */     }
/*  54: 57 */     if (paramEditor == null) {
/*  55: 57 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/lang/regexp/surroundWith/GroupSurrounder", "surroundElements" }));
/*  56:    */     }
/*  57: 57 */     if (paramArrayOfPsiElement == null) {
/*  58: 57 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "2", "org/intellij/lang/regexp/surroundWith/GroupSurrounder", "surroundElements" }));
/*  59:    */     }
/*  60: 57 */     assert ((paramArrayOfPsiElement.length == 1) || (PsiTreeUtil.findCommonParent(paramArrayOfPsiElement) == paramArrayOfPsiElement[0].getParent()));
/*  61: 58 */     PsiElement localPsiElement = paramArrayOfPsiElement[0];
/*  62: 59 */     ASTNode localASTNode1 = localPsiElement.getNode();
/*  63: 60 */     assert (localASTNode1 != null);
/*  64:    */     
/*  65: 62 */     ASTNode localASTNode2 = localASTNode1.getTreeParent();
/*  66:    */     
/*  67: 64 */     StringBuilder localStringBuilder = new StringBuilder();
/*  68: 65 */     for (int i = 0; i < paramArrayOfPsiElement.length; i++)
/*  69:    */     {
/*  70: 66 */       localObject1 = paramArrayOfPsiElement[i];
/*  71: 67 */       if ((localObject1 instanceof RegExpElementImpl)) {
/*  72: 68 */         localStringBuilder.append(((RegExpElementImpl)localObject1).getUnescapedText());
/*  73:    */       } else {
/*  74: 70 */         localStringBuilder.append(((PsiElement)localObject1).getText());
/*  75:    */       }
/*  76: 72 */       if (i > 0)
/*  77:    */       {
/*  78: 73 */         localObject2 = ((PsiElement)localObject1).getNode();
/*  79: 74 */         assert (localObject2 != null);
/*  80: 75 */         localASTNode2.removeChild((ASTNode)localObject2);
/*  81:    */       }
/*  82:    */     }
/*  83: 78 */     PsiFileFactory localPsiFileFactory = PsiFileFactory.getInstance(paramProject);
/*  84:    */     
/*  85: 80 */     Object localObject1 = localPsiFileFactory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, makeReplacement(localStringBuilder));
/*  86: 81 */     Object localObject2 = (RegExpPattern)PsiTreeUtil.getChildOfType((PsiElement)localObject1, RegExpPattern.class);
/*  87: 82 */     assert (localObject2 != null);
/*  88:    */     
/*  89: 84 */     RegExpAtom localRegExpAtom = localObject2.getBranches()[0].getAtoms()[0];
/*  90: 86 */     if (a(localPsiElement))
/*  91:    */     {
/*  92: 87 */       localObject3 = paramEditor.getDocument();
/*  93: 88 */       PsiDocumentManager.getInstance(paramProject).doPostponedOperationsAndUnblockDocument((Document)localObject3);
/*  94: 89 */       TextRange localTextRange = localPsiElement.getTextRange();
/*  95: 90 */       ((Document)localObject3).replaceString(localTextRange.getStartOffset(), localTextRange.getEndOffset(), StringUtil.escapeStringCharacters(localRegExpAtom.getText()));
/*  96:    */       
/*  97:    */ 
/*  98: 93 */       return TextRange.from(localPsiElement.getTextRange().getEndOffset(), 0);
/*  99:    */     }
/* 100: 95 */     Object localObject3 = localPsiElement.replace(localRegExpAtom);
/* 101: 96 */     return TextRange.from(((PsiElement)localObject3).getTextRange().getEndOffset(), 0);
/* 102:    */   }
/* 103:    */   
/* 104:    */   private static boolean a(PsiElement paramPsiElement)
/* 105:    */   {
/* 106:101 */     while (paramPsiElement != null)
/* 107:    */     {
/* 108:102 */       if (RegExpElementImpl.isLiteralExpression(paramPsiElement)) {
/* 109:103 */         return true;
/* 110:    */       }
/* 111:105 */       paramPsiElement = paramPsiElement.getContext();
/* 112:    */     }
/* 113:107 */     return false;
/* 114:    */   }
/* 115:    */   
/* 116:    */   protected String makeReplacement(StringBuilder paramStringBuilder)
/* 117:    */   {
/* 118:111 */     return this.b + paramStringBuilder + ")";
/* 119:    */   }
/* 120:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.lang.regexp.surroundWith.GroupSurrounder

 * JD-Core Version:    0.7.0.1

 */